package filter;

import java.io.IOException;

import java.io.IOException;   
import java.util.ArrayList;
import java.util.List;
 
import javax.servlet.Filter;   
import javax.servlet.FilterChain;   
import javax.servlet.FilterConfig;   
import javax.servlet.ServletException;   
import javax.servlet.ServletRequest;   
import javax.servlet.ServletResponse;   
import javax.servlet.http.Cookie;   
import javax.servlet.http.HttpServletRequest;   
import javax.servlet.http.HttpServletResponse; 

import dao.oc_user_name.Dao_Oc_User_Name;
import entitybean.oc_user_name.EntityBeanOc_User_Name;



import tools.ApplicationContextHelp;

public class IndexFilter implements Filter{
	  public void destroy() {   

		    
		    }   
		  
		   public void doFilter(ServletRequest arg0, ServletResponse arg1,   
		            FilterChain arg2) throws IOException, ServletException {   
		       HttpServletRequest request = (HttpServletRequest) arg0;   
		        HttpServletResponse response = (HttpServletResponse) arg1;   
		        Cookie[] cookies = request.getCookies();   
		        String[] cooks = null;   
		       String username = null;   
		       String password = null;   
		        if (cookies != null) {   
		            for (Cookie coo : cookies) {   
		               String aa = coo.getValue();   
		              cooks = aa.split("==");   
		               if (cooks.length == 2) {   
		                   username = cooks[0];   
		                   password = cooks[1];   
		               }   
		           }   
		       }  
		        
		    		List<String> s_list = new ArrayList<String>();
		    		s_list.add("applicationContext.xml");
		    		s_list.add("app-config/app_dao_l_sequence.xml");
		    		s_list.add("app-config/app_dao_oc_boat.xml");
		    		s_list.add("app-config/app_dao_oc_depthlist.xml");
		    		s_list.add("app-config/app_dao_oc_operator_user.xml");
		    		s_list.add("app-config/app_dao_oc_property_tools.xml");
		    		s_list.add("app-config/app_dao_oc_property.xml");
		    		s_list.add("app-config/app_dao_oc_station.xml");
		    		s_list.add("app-config/app_dao_oc_user_name.xml");
		    		s_list.add("app-config/app_dao_oc_voyage.xml");
		    		s_list.add("app-config/app_dao_system_log.xml");
		    		s_list.add("app-config/app_dao_systemtree.xml");
		    		ApplicationContextHelp help = new ApplicationContextHelp(s_list);
		    		Dao_Oc_User_Name dao_oc_user_name = (Dao_Oc_User_Name) help.getCtx().getBean("dao_oc_user_name");
		    		EntityBeanOc_User_Name e = new EntityBeanOc_User_Name();
		    		e=dao_oc_user_name.Find_EntityBeanOc_User_Name(username,password);
		        	 if(username.equals("admin")){
//		 			request.getSession().setAttribute("listSystemtree",
//							entitybeanSystemtreelist);
//					request.getSession().setAttribute("RolePower", powerset);
//					request.getSession().setAttribute("Ocean_User", e);
//					request.getSession().setAttribute("Ocean_Login", "Yes");	
//					request.getSession().setAttribute("IsAdmin", "Yes");
		             response.sendRedirect("/index.jsp");   
		            //request.getRequestDispatcher("/main.jsp").forward(request, response);   
		        }else{   
		             arg2.doFilter(request,response );   
		       }   
		       
		 
		    }   
		  
		     public void init(FilterConfig arg0) throws ServletException {   
		        // TODO Auto-generated method stub   
		 
		    }   

}
